$(function () {
	var id = sessionStorage.getItem(id);
	var token = sessionStorage.getItem(token);

	$("form").on("submit", function (event) {
		event.preventDefault();
		var oldpassword = document.querySelector(".oldpassword").value;
		var newpassword = document.querySelector(".newpassword").value;
		var rnewpassword = document.querySelector(".rnewpassword").value;
		if (!oldpassword || !newpassword || !rnewpassword) {
			$(".error").text("请填写所有必填项").show();
			return false;
		}
		if (newpassword !== rnewpassword) {
			$(".error").text("两次新密码输入不一致").show();
			return false;
		}

		axios
			.post(
				`http://localhost:9000/users/rpwd`,
				{
					id: sessionStorage.getItem("id"),
					oldPassword: oldpassword,
					newPassword: newpassword,
					rNewPassword: rnewpassword,
				},
				{
					headers: {
						authorization: sessionStorage.getItem("token"),
					},
				}
			)
			.then(function (response) {
				if (response.data.code === 1) {
					$(".error").text(response.data.message).show();
					sessionStorage.clear();
					window.location.href = "./login.html";
				} else {
					$(".error").text(response.data.message);
				}
			})
			.catch(function (error) {
				$(".error").text("请求出错").show();
			});
	});
});
